package com.taotao.job.core.util;

import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 洗牌算法
 *
 * @author fanbeibei
 * @date 2021/2/22 23:43
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ShuffleCardUtil {

    public static <T> List<T> shuffleCards(List<T> cards) {
        Preconditions.checkArgument(null != cards && !cards.isEmpty());
        if (cards.size() == 1) {
            return cards;
        }

        //遍历每一张牌与随机位置交换
        for (int i = 0, len = cards.size(); i < len; i++) {
            int targetIndex = RadmonUtil.nextInt(0, len);
            T ti = cards.get(i);
            T tt = cards.get(targetIndex);
            cards.set(i, tt);
            cards.set(targetIndex, ti);
        }

        return cards;
    }
}
